/**
 * 封装了jquery的get、post、put、delete请求
 * 依赖于notice模块
 * @author alexchen
 * @version 1.0
 */
layui.define(['notice', 'jquery', 'layer'], function (exports) {
    var $ = layui.$,
        layer = layui.layer,
        notice = layui.notice;

    var self = {}

    var headers = {}
    if(layui.data('token').token) {
        headers = {'Authorization':'Bearer '+layui.data('token').token};
    }

    /**
     * ajax get请求
     */
    self.get = function (url, params, success, fail) {
        $.ajax({
            url: url,
            method: 'get',
            headers: headers,
            data: params,
            success: (res) => {
                resolveResponse(res, success, fail, true);
            }
        });
    };

    /**
     * ajax get请求 不需要成功提示
     * 一般用来加载select，tag select， tree
     */
    self.getWithoutNotice = function (url, params, success) {
        $.ajax({
            url: url,
            method: 'get',
            headers: headers,
            data: params,
            success: (res) => {
                resolveResponse(res, success, {}, false);
            }
        });
    };

    /**
     * ajax post请求
     */
    self.post = function (url, params, success, fail) {
        $.ajax({
            url: url,
            method: 'post',
            headers: headers,
            data: params,
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: (res) => {
                resolveResponse(res, success, fail, true);
            }
        });
    };

    /**
     * ajax put请求
     */
    self.put = function (url, params, success, fail) {
        $.ajax({
            url: url,
            method: 'put',
            headers: headers,
            data: params,
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: (res) => {
                resolveResponse(res, success, fail, true);
            }
        });
    };

    /**
     * ajax delete请求
     */
    self.delete = function (url, params, success, fail) {
        $.ajax({
            url: url,
            headers: headers,
            method: 'delete',
            success: (res) => {
                resolveResponse(res, success, fail, true);
            }
        });
    };

    /**
     * ajax batch delete请求
     */
    self.batchDelete = function (url, params, success, fail) {
        $.ajax({
            url: url,
            headers: headers,
            data: params,
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            method: 'delete',
            success: (res) => {
                resolveResponse(res, success, fail, true);
            }
        });
    };

    function resolveResponse(r, s, f, n) {
        if (r.code === 1) {
            if (n==true) {
                notice.success(r.msg ? r.msg : '操作成功');
            }
            s(r) && (s)();
        } else if (r.code === 401) {
            layer.alert('登录已失效，请重新登录', {icon: 2, closeBtn: 0}, function () {
                window.location.href = '/manage';
            });
        } else if (r.code === 403) {
            notice.warning(r.msg ? r.msg : '对不起，您暂无该操作权限');
            f(r) && (f)();
        } else {
            notice.error(r.msg ? r.msg : '操作失败');
            f(r) && (f)();
        }
    }

    exports("httpClient", self);
});